package org.ptkparty.client.view.games;

import java.util.List;

import org.ptkparty.client.activity.AddGameActivity;
import org.ptkparty.client.model.GameProxy;
import org.ptkparty.client.model.PlaceProxy;
import org.ptkparty.client.model.UserProxy;
import org.ptkparty.client.ui.CustomValueBoxEditorDecorator;
import org.ptkparty.client.ui.PlaceListBox;
import org.ptkparty.client.ui.UserSelectionCellListView;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;

public class AddGameView extends Composite implements Editor<GameProxy> {

	private static AddGameViewUiBinder uiBinder = GWT.create(AddGameViewUiBinder.class);

	interface AddGameViewUiBinder extends UiBinder<Widget, AddGameView> {
	}

	private AddGameActivity activity;
	private ListDataProvider<UserProxy> provider;

	@UiField(provided = true)
	@Path("teamOnePlayers")
	UserSelectionCellListView teamOneList;

	@UiField(provided = true)
	@Path("teamTwoPlayers")
	UserSelectionCellListView teamTwoList;

	@UiField(provided = true)
	@Path("place")
	PlaceListBox boulodromeList;

	@UiField
	Button saveBtn;

	@UiHandler("saveBtn")
	public void onSaveBtnClicked(ClickEvent event) {
		activity.save();
	}

	@UiField
	@Path("scoreTeamOne")
	CustomValueBoxEditorDecorator<Integer> teamOneScoreIb;

	@UiField
	@Path("scoreTeamTwo")
	CustomValueBoxEditorDecorator<Integer> teamTwoScoreIb;

	public AddGameView() {
		teamOneList = new UserSelectionCellListView(new MultiSelectionModel<UserProxy>());
		teamTwoList = new UserSelectionCellListView(new MultiSelectionModel<UserProxy>());

		provider = new ListDataProvider<UserProxy>();
		provider.addDataDisplay(teamOneList);
		teamOneList.setProvider(provider);
		provider.addDataDisplay(teamTwoList);
		teamTwoList.setProvider(provider);

		boulodromeList = new PlaceListBox();

		initWidget(uiBinder.createAndBindUi(this));
	}

	public void setPresenter(AddGameActivity addGameActivity) {
		activity = addGameActivity;
	}

	public void loadUser(List<UserProxy> response) {
		provider.setList(response);
	}

	public void loadPlaces(List<PlaceProxy> places) {
		if (places != null && !places.isEmpty()) {
			boulodromeList.setValues(places);
			boulodromeList.setSelectedIndex(0);
		}
	}

}
